//
//  NewFeatureCollectionView.swift
//  weibo
//
//  Created by 张宏芝 on 15/9/4.
//  Copyright © 2015年 kama. All rights reserved.
//

import UIKit

// Cell可重用标识符
private let reuseID = "reuseID"
// 图片的数量
private let NewFeatureCount = 4
// 新特性控制器
class NewFeatureCollectionView: UICollectionViewController {

    init(){
        super.init(collectionViewLayout: UICollectionViewFlowLayout())
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
       self.collectionView?.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseID)
        prepareLayout()
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
//    准备视图布局
    private func prepareLayout(){
        
        let layout = collectionView!.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
    
// MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
         return NewFeatureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as! NewFeatureCell
        cell.imageIndex = indexPath.item
        return cell
        
    }

    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let path = collectionView.indexPathsForVisibleItems().last!
        if path.item == NewFeatureCount - 1 {
            let cell = collectionView.cellForItemAtIndexPath(path) as!NewFeatureCell
            cell.showStartButton()
        }
    }
    
}

// 新特性cell
private class NewFeatureCell:UICollectionViewCell{
// 图像索引
   private var imageIndex: Int = 0 {
        didSet{
             imageView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
    
/// 点击开始体验按钮
    @objc func clickStartButton(){
        
        NSNotificationCenter.defaultCenter().postNotificationName(HMSwitchRootViewControllerNotification, object: nil)
    }
    
/// 显示体验按钮
    private func showStartButton(){
        startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        setupUI()
    }

    private func setupUI(){
        addSubview(imageView)
        addSubview(startButton)
        
        imageView.frame = bounds
        
        // 自动布局
        startButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160))
    }
    
    
    
/// 懒加载控件
//    图像视图
    private lazy var imageView = UIImageView()
//    开始体验按钮
    private lazy var startButton:UIButton = {
        let btn = UIButton()
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        btn.addTarget(self, action:"clickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
